package com.itheima.day12_多线程.Test4;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
4、【编号2004】有一个抽奖池，里面存放了一些固定金额的奖金
int[] prizePool = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
此时，有两个人A和B轮流在奖箱中随机抽奖（即A抽一次-->B抽一次-->A抽一次-->B抽一次...），直到所有的奖金都被抽出，每次抽奖耗时1秒。请编写程序，使用两个线程模拟两人的抽奖过程，输出结果如下（金额的顺序随机）：
A抽出奖金600
B抽出奖金1000
A抽出奖金900
B抽出奖金300
A抽出奖金500
B抽出奖金200
A抽出奖金700
B抽出奖金100
A抽出奖金800
B抽出奖金400
 */
public class Demo4 {
    static int[] prizePool = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
    static ArrayList<Integer> list = new ArrayList<>();

    public static void main(String[] args) {
        Random r = new Random();
        for (int i : prizePool) {
            list.add(i);
        }
        //生产者线程
        new Thread(()->{
            while (true){
                //上锁
                synchronized("锁"){
                   if(list.size()==0){
                       break;
                   } else{
                       if(!box.flag){
                           try {
                               Thread.sleep(1000);
                           } catch (InterruptedException e) {
                               e.printStackTrace();
                           }
                           int index = r.nextInt(list.size());
                           System.out.println(Thread.currentThread().getName()+"抽出奖金"+list.get(index));
                           list.remove(index);
                          box.flag = true;
                           "锁".notify();
                       }else {
                           try {
                               "锁".wait();
                           } catch (InterruptedException e) {
                               e.printStackTrace();
                           }
                       }
                   }
                }
            }
        },"A").start();

        new Thread(()->{
            while (true){
                synchronized ("锁"){
                    if(list.size() ==0){
                        break;
                    }else{
                        if(box.flag){
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            int index = r.nextInt(list.size());
                            System.out.println(Thread.currentThread().getName()+"抽出奖金"+list.get(index));
                            list.remove(index);
                            box.flag=false;
                            "锁".notify();
                        }else{
                            try {
                                "锁".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        },"B").start();

    }
}

class box {
    public static boolean flag = false;
}
